<template>
  <div class="layout-all" style="width:30%">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="秒杀类名:" prop="title">
        <el-input v-model="form.title" placeholder="请输入广告名" />
      </el-form-item>
      <el-form-item label="封面图:" prop="thumb">
        <upload-pic :thumb="form.thumb" @update:thumb="form.thumb=$event" width="100"/>
        
      </el-form-item>
      <el-form-item label="开始时间:" prop="start_time">
        <el-date-picker
          v-model="form.start_time"
          type="datetime"
          placeholder="选择开始时间"
          default-time="12:00:00"
          @change="changeStartTime()"
        />
      </el-form-item>
      <el-form-item label="结束时间:" prop="end_time">
        <el-date-picker
          v-model="form.end_time"
          type="datetime"
          placeholder="选择结束时间"
          default-time="12:00:00"
          @change="changeEndTime()"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-close" circle @click="bk()" />
        <el-button
          type="success"
          icon="el-icon-check"
          circle
          @click="onSubmit('form')"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: 0,
      options: [],
      thumb: '',
      form: {},
      rules: {
        title: [
          { required: true, message: '请输入广告名', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '请输入开始时间', trigger: 'blur' }
        ],
        end_time: [
          { required: true, message: '请输入结束时间', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.id = this.$route.query.id
    }
    this.init_into()
  },
  methods: {
    changeStartTime() {
      const date = new Date(this.form.start_time)
      this.form.start_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },
    changeEndTime() {
      const date = new Date(this.form.end_time)
      this.form.end_time = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds()
    },

    // 提交
    onSubmit(formName) {
      const form = this.form
      console.log(form)
      this.$refs[formName].validate(valid => {
        if (valid) {
          form.id = this.id
          this.get('/shop/GoodsSpecialCate/save', form, 'POST').then(e => {})
          this.$router.back()
        } else {
          console.log('error submit!!')
          return false
        }
      })
      console.log('submit!')
    },
    // 返回
    bk() {
      this.$router.back()
    },
    // 获取当前数据
    init_into() {
      this.get('/shop/GoodsSpecialCate/info?id=' + this.id, 'POST').then(e => {
        const tpl = e.info.tpl
        this.form = tpl
        this.thumb = this.form.thumb
      })
    }
  }
}
</script>

<style>
.layout-all {
  margin: 10px;
}
</style>
